package com.qyhl.question.question;


import com.qyhl.question.common.QuestionUrl;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.CashLimitBean;
import com.qyhl.webtv.commonlib.entity.question.QuestionHomeBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

public class QuestionHomeModel implements QuestionHomeContract.QuestionHomeModel {
    private QuestionHomePresenter mPresenter;

    QuestionHomeModel(QuestionHomePresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getData() {
        Map<String, String> map = DESedeUtil.encodeMapUrl(QuestionUrl.BASE_QUESTION_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.QUESTION_VERSION);
            object.put("name", "activityInfo");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<QuestionHomeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 200) {
                            mPresenter.setError("暂无任何活动！");
                        } else {
                            mPresenter.setError(e.getMessage());
                        }

                    }

                    @Override
                    public void onSuccess(QuestionHomeBean questionHomeBean) {
                        mPresenter.setData(questionHomeBean);
                    }
                });
    }
}
